package fr.umlv.m2.recipes.ideas.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import fr.umlv.m2.recipes.ideas.bean.StepBean;
import fr.umlv.m2.recipes.ideas.dao.interfaces.RecipesConcreteDAOInterface;
import fr.umlv.m2.recipes.ideas.table.enumerations.TableName;

/**
 * DAO uses to handle an step
 * @author Christophe Chillan
 *
 */
public class StepDAO implements RecipesConcreteDAOInterface<StepBean> {
	
private static final String TABLE_STEP = TableName.STEP.toString();
	
	private static final String COL_ID = "ID";	
	private static final String COL_NAME = "NAME";
	private static final String COL_DESCRIPTION = "DESCRIPTION";
	private static final String COL_DURATION = "DURATION";
	private static final String COL_ID_RECIPES = "RECIPES_ID";
	private static final String FOREIGN_RECIPES_ID_REFERENCES="ID";

	private static final int NUM_COL_ID = 0;
	private static final int NUM_COL_NAME = 1;
	private static final int NUM_COL_DESCRIPTION = 2;
	private static final int NUM_COL_DURATION = 3;
	private final StringBuilder CREATE_DB;
	
	/**
	 * Create a step DAO
	 */
	public StepDAO(){
		CREATE_DB = new StringBuilder();
		CREATE_DB.append("CREATE TABLE ");
		CREATE_DB.append(TABLE_STEP).append(" ");
		CREATE_DB.append("(");
		CREATE_DB.append(COL_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
		CREATE_DB.append(COL_NAME).append(" TEXT, ");
		CREATE_DB.append(COL_DESCRIPTION).append(" TEXT, ");
		CREATE_DB.append(COL_DURATION).append(" INTEGER, ");
		CREATE_DB.append(COL_ID_RECIPES).append(" INTEGER, ");
		CREATE_DB.append("FOREIGN KEY(").append(COL_ID_RECIPES).append(") ");
		CREATE_DB.append(" REFERENCES ").append(TableName.RECIPES.toString()).append("(");
		CREATE_DB.append(FOREIGN_RECIPES_ID_REFERENCES).append(")");
		CREATE_DB.append(");");
	}

	/**
	 * @see SQLiteOpenHelper
	 */
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_DB.toString());
		
	}

	/**
	 * @see SQLiteOpenHelper
	 */
	public void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE "+ TABLE_STEP);
		onCreate(db);
		
	}

	/**
	 * Insert an step in the database
	 *  @param db
	 *  @param step
	 *  @return long - 1 if the entry has been inserted, 0 otherwise 
	 */
	public long insert(SQLiteDatabase db, StepBean step) {
		ContentValues values = new ContentValues();
		values.put(COL_NAME, step.getName());
		values.put(COL_DESCRIPTION, step.getDescription());
		values.put(COL_DURATION, step.getDuration());
		values.put(COL_DURATION, step.getDuration());
		values.put(COL_ID_RECIPES, step.getRecipeId());
		return db.insert(TABLE_STEP, null, values);
	}

	/**
	 * update an step from the step id in parameters
	 * @param db
	 * @param step
	 * @param id
	 * @return integer - the number of entries uploaded
	 */
	public int update(SQLiteDatabase db, StepBean step, int id) {
		ContentValues values = new ContentValues();
		values.put(COL_NAME, step.getName());
		values.put(COL_DESCRIPTION, step.getDescription());
		values.put(COL_DURATION, step.getDuration());;
		return db.update(TABLE_STEP,values, COL_ID+ " = " +id, null);
	}

	/**
	 * remove a step from the id
	 * @return integer - the number of entries removed
	 */
	public int removeWithId(SQLiteDatabase db, int id) {
		return db.delete(TABLE_STEP, COL_ID+ " = "+id, null);
	}
	
	/**
	 * Get All the steps from a recipe id
	 * @param db
	 * @param recipeId
	 * @return every step of the related to the recipe id in parameter
	 */
	public List<StepBean> getStepsFromRecipeId(SQLiteDatabase db, int recipeId){
		Cursor c = db.query(TABLE_STEP, new String[]{COL_ID,COL_NAME,COL_DESCRIPTION,COL_DURATION}, COL_ID_RECIPES+ " = "+recipeId, null, null, null, null);
		return cursorToAllSteps(c);
	}

	/**
	 * Get the last id of the steps
	 * @param db
	 * @return integer - the last id inserted in the database
	 */
	public int getLastInsertIdOfStep(SQLiteDatabase db) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT max(").append(COL_ID).append(") ");
		query.append("from ").append(TABLE_STEP);
		Cursor c = db.rawQuery(query.toString(), null);
		return cursorToMaxId(c);
	}
	
	private int cursorToMaxId(Cursor c){
		if(c.getCount()==0){
			return 0;
		}
		c.moveToFirst();
		return c.getInt(NUM_COL_ID);
	}
	
	private List<StepBean> cursorToAllSteps(Cursor c) {
		List<StepBean> steps = new ArrayList<StepBean>();
		c.moveToFirst();
		while(!c.isAfterLast()){
			StepBean step = new StepBean();
			step.setId(c.getInt(NUM_COL_ID));
			step.setName(c.getString(NUM_COL_NAME));
			step.setDescription(c.getString(NUM_COL_DESCRIPTION));
			step.setDuration(c.getInt(NUM_COL_DURATION));
			steps.add(step);
			c.moveToNext();
		}
		return Collections.unmodifiableList(steps);
	}

}

